var postsData = require('../../../data/posts-data.js')

var app = getApp();

Page({

  data:{
    isPlayingMusic:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
    //ctrl + p
    var postId = options.id;
    for (var i = 0; i < postsData.postList.length; i++) {
      if (postsData.postList[i] && postsData.postList[i].postId == postId) {
        this.setData({
          postData: postsData.postList[i]
        });
      }
    }

    var postsCollected = wx.getStorageSync('postsCollected');
    if (postsCollected) {
      var collected = postsCollected[postId];
      this.setData({
        collected: collected
      });
    } else {
      var postsCollected = {};
      postsCollected[postId] = false;
      wx.setStorageSync('postsCollected', postsCollected)
    }

    this.setMusicMonitor();

    var globalData = app.globalData;
    if (globalData.g_isPlayingMusic &&
      globalData.g_playingMuiscId == postId){
      this.setData({
        isPlayingMusic: true
      });
    }

  },

  setMusicMonitor:function(){
    var _this = this;
    // 监听音乐播放状态
    wx.onBackgroundAudioPlay(function () {
      _this.setData({
        isPlayingMusic: true
      });
      app.globalData.g_isPlayingMusic = true;
      app.globalData.g_playingMuiscId = _this.data.postData.postId;
    })

    wx.onBackgroundAudioPause(function () {
      _this.setData({
        isPlayingMusic: false
      });
      app.globalData.g_isPlayingMusic = false;
      app.globalData.g_playingMuiscId = null;
    });
  },

  onCollectionTap: function (event) {
    this.getPostsCollectedSync();
  },

  getPostsCollectedSync: function () {
    var obj = wx.getStorageSync('postsCollected');
    var collected = obj[this.data.postData.postId];
    collected = !collected;
    obj[this.data.postData.postId] = collected;
    wx.setStorageSync('postsCollected', obj);
    this.setData({
      collected: collected
    });
    wx.showToast({
      title: collected ? '收藏成功' : '取消收藏',
      duration: 1000,
      icon: 'success',
    })
  },

  onMusicTap: function (event) {
    var postId = this.data.postData.postId;
    var post = null;
    for (var i = 0; i < postsData.postList.length; i++) {
      if (postsData.postList[i] && postsData.postList[i].postId == postId) {
        post = postsData.postList[i]
      }
    }
    var isPlayingMusic = this.data.isPlayingMusic;
    if (isPlayingMusic){
      wx.pauseBackgroundAudio();//暂停音乐
      this.setData({
        isPlayingMusic: false
      });
    }else{
      wx.playBackgroundAudio({
        title: post.music.title,
        coverImgUrl: post.music.coverImg,
        dataUrl: post.music.url,
      })
      this.setData({
        isPlayingMusic: true
      });
    }

  },

  onShareAppMessage: function (res) {
    if (res.from === 'button') {
      // 来自页面内转发按钮
      console.log(res.target)
    }
    return {
      title: '青年博客APP',
      path: '/pages/posts/post',
      success: function (res) {
        console.log('转发成功')
      },
      fail: function (res) {
        console.log('转发失败')
      }
    }
  },
  onShareTip: function () {
    // 小程序缓存最大不能超过10MB
    // wx.removeStorageSync('postObj');
    //wx.clearStorageSync();
    wx.showShareMenu({
      withShareTicket: true
    })
  },



})